package com.smx.steppingstone.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import com.smx.firetag.TemplateMolder;
import com.smx.steppingstone.renderer.HtmlRenderer;
import com.smx.steppingstone.visitors.XMLVisitor;

public class UISteppingStone extends UIComponentBase {
	public UISteppingStone() {
		super();
	}

	//Passed properties
	private String rendererType;	
	private Object object;
	private int depth=0;
	//Current state
	private Object[] _state;

	
	public int getDepth() {
		int defaultDepth=5;
		if (depth == 0) {
			return defaultDepth;
		}
		ValueExpression exp = this.getValueExpression("depth");
		if (exp != null) {
			try {
				return (Integer)exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return defaultDepth;
	}
	public String getRendererType() {
		if (rendererType != null) {
			return rendererType;
		}
		ValueExpression exp = this.getValueExpression("rendererType");
		if (exp != null) {
			try {
				return (String)exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return null;
	}

	public Object getObject() {
		if (object != null) {
			return object;
		}
		ValueExpression exp = this.getValueExpression("object");
		if (exp != null) {
			try {
				return exp.getValue(getFacesContext().getELContext());
			} catch (ELException ex) {
				throw new FacesException(ex);
			}
		}
		return null;
	}

	@Override
	public void encodeBegin(FacesContext context) throws IOException {
		assertValidInput(context);
		if (!isRendered()) {
			return;
		}
		ResponseWriter writer=context.getResponseWriter();
		InputStream stream =null;	
		try {
			XMLVisitor visitor = new XMLVisitor(this.getDepth());
			visitor.visit( this.getObject() );
			StringBuffer buff = visitor.getBuffer();
			//System.out.print(buff);
			HtmlRenderer renderer=new HtmlRenderer();
			renderer.render(buff);			
			
			String template="/templates/template.txt";
			stream = this.getClass().getResourceAsStream(template);
				
			int result=-1;
			StringBuffer sb=new StringBuffer();
			while((result=stream.read())!=-1){
				sb.append((char)result);
			}
			TemplateMolder molder=new TemplateMolder();
			molder.setTemplate(sb.toString());
			molder.addVariable("{output}", renderer.getBuffer().toString());

			writer.write(molder.applyMold());

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(stream != null){
				try {
					stream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public void encodeEnd(FacesContext context) throws IOException {
		assertValidInput(context);

		if (!isRendered()) {
			return;
		}
		super.encodeEnd(context);
		ResponseWriter writer=context.getResponseWriter();		
		String script=" ";		
		String id=this.getClientId(context);
	}



	@Override
	public String getFamily() {

		return null;
	}


	@Override
	public void restoreState(FacesContext context, Object state) {
		this._state=(Object[])state;

	}
	@Override
	public Object saveState(FacesContext context) {
		if(_state==null){
			_state=new Object[1];			
		}
		this._state[0] = super.saveState(context);
		return this._state;
	}


	/**
	 * This method checks for null context and component
	 * 
	 * @param context
	 * @param component
	 */
	private void assertValidInput(FacesContext context) {
		if (context == null) {
			throw new NullPointerException();
		} 
	}
}
